<template>
  <div class="login-wrap">
    <el-form
      label-position="left"
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="0px"
      class="demo-ruleForm login-container"
    >
      <!-- <h2 class="title">HELLO !!!</h2> -->
      <h3 class="title">鹏城测试管理后台</h3>
      <el-form-item style="margin-bottom: 30px;" prop="account">
        <el-input
          type="text"
          prefix-icon="el-icon-user"
          v-model="ruleForm.account"
          auto-complete="off"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          prefix-icon="el-icon-lock"
          v-model="ruleForm.password"
          auto-complete="off"
          placeholder="请输入密码"
          @keyup.enter.native="submitForm('ruleForm')"
        ></el-input>
      </el-form-item>
      <el-checkbox class="remember" v-model="rememberpwd">记住密码</el-checkbox>
      <el-form-item>
        <div style="width:50%; margin: 0 auto">
          <el-button
            style="width: 100%;"
            type="primary"
            @click="submitForm('ruleForm')"
            :loading="logining"
            >立即登录</el-button
          >
        </div>
      </el-form-item>
    </el-form>
    <canvas id="canvas"></canvas>
  </div>
</template>
<script>
import { login } from "../api/user";
import { getCookie, setCookie, delCookie } from "../utils/util";
// import md5 from "js-md5";
export default {
  name: "login",
  data() {
    return {
      //定义loading默认为false
      logining: false,
      // 记住密码
      rememberpwd: true,
      ruleForm: {
        account: "",
        password: ""
      },
      //rules前端验证
      rules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getuserpwd();
  },
  // 里面的函数只有调用才会执行
  methods: {
    //登录接口调用
    async _login(data) {
      let res = await login(data);
      if (res.code == 200) {
        this.logining = true;
        this.$store.commit("login", "true");
        this.$store.commit("setUserInfo", res.data);
        window.localStorage.setItem("startTime", new Date().getTime()); // 设置登入时候的时间 用来做过期时间
        if (this.rememberpwd) {
          //保存帐号到cookie，有效期30天
          setCookie("user", this.ruleForm.account, 30);
          //保存密码到cookie，有效期30天
          setCookie("pwd", this.ruleForm.password, 30);
        } else {
          delCookie("user");
          delCookie("pwd");
        }
        // 测试通道，不为空直接登录
        setTimeout(() => {
          this.logining = false;
          this.$router.push({ path: "/system/user" });
          this.$message.success("登入成功！");
        }, 500);
      }
    },

    // 获取用户名密码
    getuserpwd() {
      if (getCookie("user") != "" && getCookie("pwd") != "") {
        this.ruleForm.account = getCookie("user");
        this.ruleForm.password = getCookie("pwd");
      }
    },

    //获取info列表
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this._login(this.ruleForm);
        } else {
          this.$message.error("请输入用户名密码！");
          this.logining = false;
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 15%;
  background-image: url("http://applet.atptest.com/Bpsu5AmF1686037868296.jpg?v=1686037868660");
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100% 100%;
}

.login-container {
  margin: 0 auto;
  border-radius: 10px;
  /* margin: 0px auto; */
  width: 450px;
  padding: 30px 35px 15px 35px;
  /* background: #ffffff; */
  /* background-color: rgba(64, 64, 119, 0.5) !important; */
  background-image: url("http://applet.atptest.com/Bpsu5AmF1686037868296.jpg?v=1686037868660");
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center;

  border: 1px solid #a5b1c7;
  text-align: left;
  box-shadow: 10px 5px 20px 10px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #fff;
  font-weight: 700;
  font-size: 30px;
}

.remember {
  margin: 0px 0px 35px 0px;
}
</style>
